package com.unit.test.service.impl;

import com.unit.test.model.Billing;
import com.unit.test.model.User;
import com.unit.test.service.AccountService;
import com.unit.test.service.BillingService;
import com.unit.test.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author alexhu
 */
@Service
@Slf4j
public class AccountServiceImpl implements AccountService {

    @Autowired
    EmailService emailService;
    @Autowired
    BillingService billingService;

    @Override
    public String getInfo(String msg) {
        return "no mock : " + msg;
    }

    @Override
    public String getAccountIdByUserId(Long userId) {
        // 模拟数据
        return userId + "account_id";
    }

    /**
     * ① 通过用户id查询账户Id
     * ② 通过账户id查询最新账单信息
     * ③ 调用邮件接口，发送邮件
     * ④ 判断邮件发送状态
     *
     * @param user
     * @return
     */
    @Override
    public Boolean sendBilling(User user) {
        // 查询用户账户账单
        log.info("Query user billing......");
        String accountId = this.getAccountIdByUserId(user.getId());
        Billing billing = billingService.queryLatestBilling(accountId);
        // 发送账单邮件
        Boolean sendResult = emailService.sendmail(billing);
        // 发送成功，执行成功逻辑
        if (sendResult) {
            // 发送成功逻辑
            billing.setNotificationStatus(Boolean.TRUE);
            log.info("邮件下发账单信息成功~");
            billingService.updateBilling(billing);
            return Boolean.TRUE;
        } else {
            // 发送失败处理逻辑
            log.warn("邮件下发账单信息失败~");
            return Boolean.FALSE;
        }
    }

}
